package com.wangzh.concurrent.lock.pool;

import cn.hutool.core.util.RandomUtil;

import java.util.concurrent.*;

//Executors 工具类
public class Demo1 {

    public static void main(String[] args) {
//        ExecutorService executorService = Executors.newFixedThreadPool(3);
//        ExecutorService executorService1 = Executors.newSingleThreadExecutor();
//        ExecutorService executorService2 = Executors.newCachedThreadPool();

        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(2,4,1, TimeUnit.SECONDS
                ,new ArrayBlockingQueue<>(2),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());


        try {
            for (int i = 0; i <10 ; i++) {
                final int itemp =i;
                Future<String> submit = poolExecutor.submit(() -> {
                    System.out.println(Thread.currentThread().getName() + "---->" + itemp);
                    int i1 = RandomUtil.randomInt(10);
                    try {
                        TimeUnit.SECONDS.sleep(i1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("等待了" + i1 + "秒");
                    return "nihao";
                });

//                String s = submit.get();
//                System.out.println(i+"---"+s);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            poolExecutor.shutdown();

        }


//        try {
//            executorService.execute(() ->{
//                System.out.println("aaa");
//            });
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        } finally {
//            executorService.shutdown();
//        }
    }
}
